//
//  PopView.m
//  copyrightTrading
//
//  Created by 郭子一 on 2020/10/8.
//  Copyright © 2020 郭子一. All rights reserved.
//

#import "PopView.h"
#import "PlaceHolderTextView.h"
@interface PopView ()

@property (nonatomic, strong) PlaceHolderTextView *textView;

@end

@implementation PopView

- (instancetype)initWithStyle:(PopViewType)popViewType
{
    self = [super init];
    if (self) {
        self.popViewType = popViewType;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor =  RGB_VALUE(0x000000);
    bgView.alpha = 0.4;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 12;
    view.layer.masksToBounds = YES;
    [self addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.right.offset(-16);
        make.center.offset(0);
        make.height.mas_equalTo(343);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.font = JSS_MEDIUM_FONT(17);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(39);
        make.centerX.offset(0);
        make.left.right.offset(0);
        make.height.mas_equalTo(24);
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"popViewClose"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-27);
        make.top.offset(24);
        make.width.height.mas_equalTo(24);
    }];
    
    self.img = [[UIImageView alloc] init];
    self.img.backgroundColor = RGB_VALUE(0xE6E6E6);
    self.img.userInteractionEnabled = YES;
    self.img.layer.masksToBounds = YES;
    self.img.layer.cornerRadius = 12;
    [view addSubview:self.img];
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(14);
        make.left.offset(16);
        make.right.offset(-16);
        make.height.mas_equalTo(160);
    }];
    
    self.textView = [[PlaceHolderTextView alloc] init];
    self.textView.textColor = RGB_VALUE(0x6F6F6F);
//    self.textView.textColor = RGB_VALUE(0x6F6F6F);
    self.textView.backgroundColor = RGB_VALUE(0xE6E6E6);
    self.textView.placeholder = @"请输入申诉理由";
    [self.img addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(16);
        make.left.offset(19);
        make.right.offset(-19);
        make.bottom.offset(-16);
    }];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImg)];
    [self.img addGestureRecognizer:tapGes];
    
    self.contentLab = [UILabel new];
    self.contentLab.font = JSS_MEDIUM_FONT(12);
    self.contentLab.textAlignment = NSTextAlignmentCenter;
    self.contentLab.numberOfLines = 0;
    self.contentLab.textColor = RGB_VALUE(0x6F6F6F);
    [self.img addSubview:self.contentLab];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.left.right.offset(0);
        make.height.mas_equalTo(34);
    }];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"确定" forState:UIControlStateNormal];
    [loginBtn setTitleColor:RGB_VALUE(0xFFFFFF) forState:UIControlStateNormal];
    [loginBtn.titleLabel setFont:JSS_MEDIUM_FONT_7];
    loginBtn.backgroundColor = RGB_VALUE(0xDC0000);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.layer.cornerRadius = 12;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-15);
        make.right.offset(-81);
        make.left.offset(81);
        make.height.mas_equalTo(51);
    }];
    
    if (self.popViewType == KFaceType) {
        titleLabel.text = @"人脸图片视频搜索";
        self.contentLab.text = @"上传图片\n支持10MB以下jpeg、png、heic格式人脸图片上传";
        self.textView.hidden = YES;
    } else if (self.popViewType == KVideoType) {
        titleLabel.text = @"视频片段视频搜索";
        self.contentLab.text = @"上传视频\n支持100MB以下视频上传";
        self.textView.hidden = YES;
    } else if (self.popViewType == KPictureType) {
        titleLabel.text = @"台标图片视频搜索";
        self.contentLab.text = @"上传图片\n支持10MB以下jpeg、png格式台标图片上传";
        self.textView.hidden = YES;
    } else if (self.popViewType == KComplainType) {
        titleLabel.text = @"我要申诉";
        self.contentLab.hidden = YES;
        [loginBtn setTitle:@"提交" forState:UIControlStateNormal];
        self.textView.hidden = NO;
    }
    
}



- (void)tapImg
{
    if ([self.delegate respondsToSelector:@selector(tapImgChooseSourceType:)]) {
        if (self.popViewType == KFaceType || self.popViewType == KPictureType) {
            [self.delegate tapImgChooseSourceType:1];
        } else {
            [self.delegate tapImgChooseSourceType:2];
        }
    }
}

- (void)loginBtnClick
{
    NSString *contentString = self.popViewType==KComplainType ? self.textView.text : @"";
    if ([self.delegate respondsToSelector:@selector(PopViewEnsureClickWithType:content:)]) {
        [self.delegate PopViewEnsureClickWithType:self.popViewType content:contentString];
    }
    [self closeBtnClick];
}

- (void)closeBtnClick
{
    [UIView animateWithDuration:0.1 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
